package com.am.service.serviceImpl;

import com.am.bean.Menu;
import com.am.bean.User;
import com.am.bean.UserRole;
import com.am.dao.MenuMapper;
import com.am.dao.MenuRoleMapper;
import com.am.dao.UserMapper;
import com.am.dao.UserRoleMapper;
import com.am.service.LoginService;
import com.utils.Meg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.RoleList;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Meg login(String username, String password,String authCode, HttpSession httpSession) {
        Integer b = userMapper.selectAction(username);
        authCode = authCode.toLowerCase();
        boolean bool = authCode.equals(httpSession.getAttribute("authCode"));
        if(!bool){
            return Meg.Message(1000,"验证码错误！");
        }
        if (b == null || b == 0)
            return Meg.userAction();
        User user = userMapper.selectByUserAndPass(username, password);
        if (user != null) {
            httpSession.setAttribute("user", user);
            return Meg.loginSuccess();
        } else
            return Meg.loginFile();
    }

    @Override
    public Meg homeJson(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        List roleId = getRole(user.getId());

        return Meg.success().add("data",getMenuJson(0,roleId));
    }


    public List getRole(Integer userId){
        List roleId = new RoleList();
        List<UserRole> roleList = userRoleMapper.selectByUserId(userId);
        for (int i = 0; i < roleList.size(); i++) {
            List menuIdList = menuRoleMapper.selectByRoleId(roleList.get(i).getRoleId());
            roleId.addAll(menuIdList);
        }
        return roleId;
    }

    public List getMenuJson(Integer id, List menuIdList){
        List list = new ArrayList();
        List<Menu> menuList = menuMapper.getMenuByParentId(id);
        Map map = null;
        for (int i = 0; i < menuList.size() ; i++) {
            if(menuIdList.contains(menuList.get(i).getId())?true:false){
                map = new HashMap();
                map.put("id",menuList.get(i).getId());
                map.put("name",menuList.get(i).getMenuName());
                if(getMenuJson(menuList.get(i).getId(),menuIdList).size()>0){
                    map.put("children",getMenuJson(menuList.get(i).getId(),menuIdList));
                }else {
                    map.put("url",menuList.get(i).getMenuUrl());
                }
                list.add(map);
            }
        }
        return list;
    }


}
